package com.douding.server.service;

import com.douding.server.domain.*;
import com.douding.server.dto.*;
import com.douding.server.mapper.ChapterMapper;
import com.douding.server.util.CopyUtil;
import com.douding.server.util.UuidUtil;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class ChapterService {

    @Resource
    private ChapterMapper chapterMapper;

    public void list(ChapterPageDto chapterPageDto){
        PageHelper.startPage(chapterPageDto.getPage(), chapterPageDto.getSize());
        ChapterExample chapterExample = new ChapterExample();
        List<Chapter> chapterList = chapterMapper.selectByExample(chapterExample);
        PageInfo<Chapter> pageInfo = new PageInfo<>(chapterList);
        chapterPageDto.setTotal(pageInfo.getTotal());
        List<ChapterDto> chapterDtoList = CopyUtil.copyList(chapterList, ChapterDto.class);
        chapterPageDto.setList(chapterDtoList);
    }// method


    public void save(ChapterDto chapterDto) {
        Chapter chapter = CopyUtil.copy(chapterDto, Chapter.class);

        //判断是新增 还是修改
        if (StringUtils.isEmpty(chapterDto.getId())) {
            this.insert(chapter);
        } else {
            this.update(chapter);
        }


    }
    //新增数据
    private void insert(Chapter chapter) {

        chapter.setId(UuidUtil.getShortUuid());
        chapterMapper.insert(chapter);
    }
    //更新数据
    private void update(Chapter chapter) {
        chapterMapper.updateByPrimaryKey(chapter);
    }

    public void delete(String id) {
        chapterMapper.deleteByPrimaryKey(id);
    }

    /**
     * 查询某一课程下的所有章
     */
    public List<ChapterDto> listByCourse(String courseId) {
//        ChapterExample example = new ChapterExample();
//        example.createCriteria().andCourseIdEqualTo(courseId);
//        List<Chapter> categories = ChapterMapper.selectByExample(example);
//        return CopyUtil.copyList(categories, ChapterDto.class);
        return null;
    }
}//end class
